package Panels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import Runables.Run_principal;

import componentes.ConexionBD;
import componentes.labtext;
import componentes.panelconfondo;

public class PanelGCine extends panelconfondo implements ActionListener {

	
	
	GridBagConstraints gbc;
	labtext idcine;
	labtext nombre;
	labtext direccion;
	labtext codpostal;
	labtext telefono,director,pass;
	JButton bcontinuar,breset;
	int panel;
	JComboBox empleado;
	
	public PanelGCine(){
		super.setimagen("/recursos/cine1.jpg");
		panel = 1;
		this.dibujar();
		director = new labtext(" Director : ",1,4,15);
	    gbc = new GridBagConstraints(0, 6, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(director, gbc);
		
		pass = new labtext(" Pass : ",5,8,15);
	    gbc = new GridBagConstraints(0, 7, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(pass, gbc);
		
	}
		
	public PanelGCine(int cine){
		super.setimagen("/recursos/cine1.jpg");
		panel = 2;
		this.dibujar();	
		cargadatos(cine);
		cargaempleados();
		this.idcine.setEditable(false);
		gbc = new GridBagConstraints(0, 6, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(empleado, gbc);
		
		
	}

	public void dibujar(){
		
		this.setLayout(new GridBagLayout());
		idcine = new labtext(" Id Cine : ",2,1,6);
	    gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(idcine, gbc);
		
		nombre = new labtext(" Nombre : ",1,5,15);
	    gbc = new GridBagConstraints(0, 1, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(nombre, gbc);
		
		

		direccion = new labtext(" Direccion : ",1,8,15);
	    gbc = new GridBagConstraints(0, 3, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(direccion, gbc);
		
		codpostal = new labtext("Codigo Postal :",2,5,5);
	    gbc = new GridBagConstraints(0, 4, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(codpostal, gbc);
		

		
		telefono = new labtext(" Telefono : ",4,8,15);
	    gbc = new GridBagConstraints(0, 5, 1, 1, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(
						5, 5, 5, 5), 0, 0);
		this.add(telefono, gbc);
		
		
		
		bcontinuar = new JButton(" Continuar ");
	    gbc = new GridBagConstraints(0, 8, 0, 0, 0, 0,
				GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
						5, 5, 5, 5), 0, 0);
	    bcontinuar.addActionListener(this);
		this.add(bcontinuar, gbc);
		
		breset = new JButton(" Borrar ");
	    gbc = new GridBagConstraints(0, 8, 0, 0, 0, 0,
				GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(
						5, 5, 5, 5), 0, 0);
	    breset.addActionListener(this);
		this.add(breset, gbc);
		
	}
	
	public boolean validar(){
		
		if(panel==1 &&
			this.director.validar()==true &&	
		   this.nombre.validar()==true &&	
		   this.codpostal.validar()==true &&
		   this.direccion.validar()==true &&
		   this.telefono.validar()==true &&
		   this.pass.validar()==true) {

			return true;
		}else if(panel==2 &&	
		   this.nombre.validar()==true &&	
		   this.codpostal.validar()==true &&
		   this.direccion.validar()==true &&
		   this.telefono.validar()==true){
			return true;
		}
		
		JOptionPane.showMessageDialog(this,
			    "Hay datos mal introducidos",
			    "Mensaje Error",
			    JOptionPane.ERROR_MESSAGE);

		return false;
	}
	
	public int getidcine(){
		String consulta = "Select idcine from cine where nombre like '" + this.nombre.getText()+"' and codpost = "+this.codpostal.getText();
		System.out.println(consulta);
		try{
			ResultSet rs = ConexionBD.executeQuery(consulta);
			while(rs.next()){
				System.out.println(rs.getInt(1));
			return rs.getInt(1);
			}
		}catch(Exception e){
			JOptionPane.showMessageDialog(this,
				    "Error reciviendo la id del cine",
				    "Mensaje Error",
				    JOptionPane.ERROR_MESSAGE);
		}
		return 0;
	}
	
	
	public void cargadatos(int codigo){
		String consulta = "Select * from cine where idcine = "+codigo;

		try{
			ResultSet rs = ConexionBD.executeQuery(consulta);
			while(rs.next()){  
				   this.idcine.setText(rs.getInt(1)+"");
			       this.nombre.setText(rs.getString(2));
			       this.direccion.setText(rs.getString(3));
			       this.telefono.setText(rs.getInt(5)+"");
			       this.codpostal.setText(rs.getInt(4)+"");
			}
		}catch(Exception e){
			JOptionPane.showMessageDialog(this,
				    "Datos no cargados",
				    "Mensaje Error",
				    JOptionPane.ERROR_MESSAGE);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(panel==1){
			if (e.getSource() == bcontinuar && this.validar()==true){
				
				String consultacine="INSERT INTO cine (nombre,direccion,codpost,telefono)VALUES(\""+
			
			       this.nombre.getText()
		           +"\",\""
			       + this.direccion.getText()
			       +"\",\""
			       + this.codpostal.getText()
			        +"\",\""
			       + this.telefono.getText()+"\")";
			        	System.out.println(consultacine );
			 	
			        	
			        	
			try{
				componentes.ConexionBD.executeUpdateQuery(consultacine);
			
				JOptionPane.showMessageDialog(this,
					    "Base de datos actualizada correctamente", 
					    "Message",
					    JOptionPane.INFORMATION_MESSAGE);

			}catch(Exception ex){		
				System.out.println(ex);
				JOptionPane.showMessageDialog(this,
					    "Error al actualizar los datos en la base de datos",
					    "Mensaje Error",
					    JOptionPane.ERROR_MESSAGE);
			}
			
	       	
			   String consulta="INSERT INTO trabajador (numemp,tipo,pass,idcine)VALUES(\""+
					       this.director.getText()
			               +"\",\"Director\",\""
					       + this.pass.getText()
					        +"\",\""
						  + this.getidcine()+"\")";
					        	System.out.println(consulta);     
					        	System.out.println(this.getidcine());
			try{
			
				componentes.ConexionBD.executeUpdateQuery(consulta);
				JOptionPane.showMessageDialog(this,
					    "Base de datos actualizada correctamente", 
					    "Message",
					    JOptionPane.INFORMATION_MESSAGE);

			}catch(Exception ex){		
				System.out.println(ex);
				JOptionPane.showMessageDialog(this,
					    "Error al actualizar los datos en la base de datos",
					    "Mensaje Error",
					    JOptionPane.ERROR_MESSAGE);
			}
			}
			
       
			
		}
		
		 if(panel==2){
			
			 
			 if (e.getSource() == bcontinuar && this.validar()==true) {
				 String consulta="UPDATE cine SET idcine ="+
			        this.idcine.getText()+" ,nombre='"+
			        this.nombre.getText()+"' ,direccion='"+
			        this.direccion.getText()+"',codpost="+
	                this.codpostal.getText()+",telefono="+
	                this.telefono.getText()+" WHERE idcine ="+
	                this.idcine.getText();
			    System.out.println(consulta);
				 
			    try{
					componentes.ConexionBD.executeUpdateQuery(consulta);
					JOptionPane.showMessageDialog(this,
						    "Base de datos actializada correctamente", 
						    "Message",
						    JOptionPane.INFORMATION_MESSAGE);

				}catch(Exception ex){		
					System.out.println(ex);
					JOptionPane.showMessageDialog(this,
						    "Error al actualizar los datos en la base de datos",
						    "Mensaje Error",
						    JOptionPane.ERROR_MESSAGE);
				} 
			 }
			 
			 if (e.getSource() == breset){
				 Object[] options = {"Si",
		                    "No"};
				
				 
				 int n = JOptionPane.showOptionDialog(this,
						 "Estas seguro de que deseas eliminar el reguistro?",
						 "Confirmar",
						 JOptionPane.YES_NO_CANCEL_OPTION,
						    JOptionPane.QUESTION_MESSAGE,
						    null,
						    options,
						    options[1]);
						 
						 System.out.println(n);
						 
					if (n==0){
					String	consulta="Delete from cine where idcine ="+this.idcine.getText();
						
					try{
						componentes.ConexionBD.executeUpdateQuery(consulta);
						JOptionPane.showMessageDialog(this,
							    "Reguistro eliminado",
							    "Borrado",
							    JOptionPane.INFORMATION_MESSAGE);
					}catch(Exception ex){
						JOptionPane.showMessageDialog(this,
							    "Datos no cargados",
							    "Mensaje Error",
							    JOptionPane.ERROR_MESSAGE);
					}	
					}
			 }
			 
			 
			 
			}	
		
	}

	
	public void cargaempleados(){
		
		String consultaempleados = new String();
		
		if(Run_principal.user.getTipo().equals("Admin")){
			consultaempleados = "Select nombre from trabajador where tipo like \"Director\"";
		}

		if(Run_principal.user.getTipo().equals("Director")){
			consultaempleados = "Select nombre from trabajador where tipo like \"Director\" and idcine ="+ Run_principal.user.getIdcine();
		}
		
		empleado = new JComboBox();
		try {
			ResultSet rs = ConexionBD.executeQuery(consultaempleados);
		    
			while(rs.next()){
		    	empleado.addItem(rs.getString(1));
		    }
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
